package com.sunshine.controller.admin;

import com.sunshine.bean.Article;
import com.sunshine.model.ArticleMapper;
import com.sunshine.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.sunshine.util.Result.json;

/**
 * Created by along on 12/4/17.
 */
@RestController
@RequestMapping("/admin/article")
public class AdminArticleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminArticleController.class);

    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping
    public Map<String, Object> create(@RequestParam(value = "title") String title,
                                      @RequestParam(value = "content") String content,
                                      @RequestParam(value = "type") String type,
                                      @RequestParam(value = "attachment", required = false) String attachment){
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setType(type);
        article.setAttachment(attachment);

        articleMapper.insert(article);
        return json(Result.STATUS_CODE.SUCCESS);
    }

    @DeleteMapping
    public Map<String, Object> delete(@RequestParam(value = "id") int id){
        articleMapper.delete(id);
        return json(Result.STATUS_CODE.SUCCESS);
    }
}
